﻿package mortal.game.view.common.button
{
    import com.mui.controls.*;
    import com.mui.core.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.cd.effect.*;

    public class TimeLoadingButton extends GLoadingButton
    {
        private var _cdTime:int;
        private var _templabel:String = "";
        private var _type:String;
        private var _freezingEffect:CDFreezingEffect;
        private var _leftTimeEffect:CDLeftTimeEffect;
        private var _cdData:ICDData;
        private var _cacheName:String;
        private var _isByClick:Boolean = true;
        private var _labelBitmap:GBitmap;
        public static const COUNTDOWN:String = "countDown";
        public static const CD:String = "cd";

        public function TimeLoadingButton(param1:int = 30000, param2:String = "countDown", param3:String = "")
        {
            this._cdTime = param1;
            this._type = param2;
            this.cacheName = param3;
            this.addEventListener(MouseEvent.CLICK, this.clickHandler);
            this.addEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.removeFromStageHandler);
            return;
        }// end function

        public function get isByClick() : Boolean
        {
            return this._isByClick;
        }// end function

        public function set isByClick(param1:Boolean) : void
        {
            this._isByClick = param1;
            return;
        }// end function

        public function get type() : String
        {
            return this._type;
        }// end function

        public function set type(param1:String) : void
        {
            this._type = param1;
            return;
        }// end function

        private function addToStageHandler(event:Event) : void
        {
            this.registerEffects();
            this.updateCDStatus();
            return;
        }// end function

        private function removeFromStageHandler(event:Event) : void
        {
            this.unRegisterEffects();
            return;
        }// end function

        private function cdEndHandler() : void
        {
            this.enabled = true;
            this.filters = [];
            super.label = this._templabel;
            if (this._labelBitmap)
            {
                this._labelBitmap.visible = true;
            }
            return;
        }// end function

        protected function registerEffects() : void
        {
            var _loc_1:TextFormat = null;
            if (this._cdData == null)
            {
                return;
            }
            if (this._type == CD)
            {
                if (this._freezingEffect == null)
                {
                    this._freezingEffect = new CDFreezingEffect();
                    this.resetEffectPlace();
                }
                this._cdData.addEffect(this._freezingEffect);
                this.addChild(this._freezingEffect);
            }
            else if (this._type == COUNTDOWN)
            {
                if (this._leftTimeEffect == null)
                {
                    this._leftTimeEffect = new CDLeftTimeEffect();
                    this._leftTimeEffect.filters = [FilterConst.glowFilter];
                    _loc_1 = GlobalStyle.textFormatHuang;
                    _loc_1.align = TextFormatAlign.CENTER;
                    this._leftTimeEffect.defaultTextFormat = _loc_1;
                    this.resetEffectPlace();
                }
                this._cdData.addEffect(this._leftTimeEffect);
                this.addChild(this._leftTimeEffect);
            }
            this._cdData.addFinishCallback(this.cdEndHandler);
            this._cdData.addStartCallback(this.updateCDStatus);
            return;
        }// end function

        protected function unRegisterEffects() : void
        {
            if (this._cdData == null)
            {
                return;
            }
            if (this._freezingEffect && this._freezingEffect.registed)
            {
                this._cdData.removeEffect(this._freezingEffect);
            }
            if (this._leftTimeEffect && this._leftTimeEffect.registed)
            {
                this._cdData.removeEffect(this._leftTimeEffect);
            }
            DisplayUtil.removeMe(this._freezingEffect);
            DisplayUtil.removeMe(this._leftTimeEffect);
            this._cdData.removeFinishCallback(this.cdEndHandler);
            this._cdData.removeStartCallback(this.updateCDStatus);
            return;
        }// end function

        private function resetEffectPlace() : void
        {
            if (this._freezingEffect)
            {
                this._freezingEffect.x = _width / 2;
                this._freezingEffect.y = _height / 2;
                this._freezingEffect.setMaskSize(_width, _height);
            }
            if (this._leftTimeEffect)
            {
                this._leftTimeEffect.width = _width;
                this._leftTimeEffect.height = 18;
                this._leftTimeEffect.x = 0;
                this._leftTimeEffect.y = (_height - this._leftTimeEffect.height) / 2;
            }
            if (this._labelBitmap)
            {
                this._labelBitmap.x = (_width - this._labelBitmap.width) / 2;
                this._labelBitmap.y = (_height - this._labelBitmap.height) / 2;
            }
            return;
        }// end function

        public function set cacheName(param1:String) : void
        {
            this._cacheName = param1;
            if (param1 == null || param1 == "")
            {
                if (this._cdData == null)
                {
                    this._cdData = new CDData();
                }
                this._cdData.totalTime = this._cdTime;
                return;
            }
            var _loc_2:* = Cache.instance.cd.getCDData(this._cacheName, CDDataType.timeButton);
            if (_loc_2 == null)
            {
                this._cdData = new CDData();
                this._cdData.totalTime = this._cdTime;
                Cache.instance.cd.registerCDData(CDDataType.timeButton, this._cacheName, this._cdData);
            }
            else
            {
                this._cdData = _loc_2;
                this._cdData.totalTime = this._cdTime;
                this.updateCDStatus();
            }
            return;
        }// end function

        private function updateCDStatus() : void
        {
            this.resetEffectPlace();
            if (!this._cdData.isCoolDown)
            {
                if (this._freezingEffect)
                {
                    this._freezingEffect.reset();
                }
                if (this._leftTimeEffect)
                {
                    this._leftTimeEffect.reset();
                }
                this.cdEndHandler();
                return;
            }
            if (this._type == COUNTDOWN)
            {
                super.label = "";
                if (this._labelBitmap)
                {
                    this._labelBitmap.visible = false;
                }
            }
            this.enabled = false;
            this.filters = [FilterConst.colorFilter2];
            return;
        }// end function

        public function set cdTime(param1:int) : void
        {
            this._cdTime = param1;
            this.cacheName = this._cacheName;
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            if (!this._isByClick)
            {
                return;
            }
            this._cdData.startCoolDown();
            this.updateCDStatus();
            return;
        }// end function

        public function startCoolDown() : void
        {
            if (this._cdData == null)
            {
                return;
            }
            this._cdData.startCoolDown();
            this.updateCDStatus();
            return;
        }// end function

        public function get running() : Boolean
        {
            return this._cdData.isCoolDown;
        }// end function

        override public function set label(param1:String) : void
        {
            this._templabel = param1;
            super.label = param1;
            this.updateCDStatus();
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.unRegisterEffects();
            if (this._cacheName == null || this._cacheName == "")
            {
                this._cdData.stopCoolDown();
            }
            if (this._labelBitmap)
            {
                this._labelBitmap.dispose(param1);
                this._labelBitmap = null;
            }
            if (this._leftTimeEffect)
            {
                this._leftTimeEffect.text = "";
            }
            this._cdData = null;
            this._cdTime = 30000;
            this._cacheName = null;
            this._type = TimeButton.COUNTDOWN;
            this._isByClick = true;
            this.enabled = true;
            this.filters = [];
            super.dispose(param1);
            return;
        }// end function

        public function set labelBitmap(param1:String) : void
        {
            if (param1)
            {
                if (this._labelBitmap)
                {
                    this._labelBitmap.bitmapData = GlobalClass.getBitmapData(param1);
                }
                else
                {
                    this._labelBitmap = GlobalClass.getBitmap(param1);
                    this.addChild(this._labelBitmap);
                }
            }
            this.resetEffectPlace();
            return;
        }// end function

    }
}
